package com.wyp168.serviceucenter.controller;


import com.wyp168.commonutils.JwtUtils;
import com.wyp168.commonutils.R;
import com.wyp168.commonutils.ordervo.UCenterMemberOrder;
import com.wyp168.serviceucenter.entity.UcenterMember;
import com.wyp168.serviceucenter.entity.vo.RegisterVo;
import com.wyp168.serviceucenter.service.UcenterMemberService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author wyp168
 * @since 2022-05-24
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
@Slf4j
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {

        //调用service方法实现登录
        //返回token值，使用jwt生成


        String token = memberService.login(member);
        System.out.println("******" + token);
        return R.ok().data("token",token);

    }


    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    //获取登录用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        System.out.println(request);
        //调用Jwt工具类中的方法，解析request数据，获取memberIda
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println("********" + memberId);
        log.debug("获取到的memberId为：{}",memberId);
        //根据memberId到表中查找对应用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    //根据用户id获取用户信息
    @GetMapping("getUserInfoOrder/{id}")
    public UCenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        UCenterMemberOrder uCenterMemberOrder = new UCenterMemberOrder();
        //把member中的值赋值给UCenterMemberOrder
        BeanUtils.copyProperties(member,uCenterMemberOrder);
        System.out.println("____++++" + uCenterMemberOrder);
        return uCenterMemberOrder;
    }

    //查询某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegister(day);
        return R.ok().data("registerCount",count);
    }
}

